<cfcomponent output="false">
	 
	<cfif structKeyExists(URL, "libid")>
		<cfset URL.method = "getLibrary" />
	<cfelseif structKeyExists(URL, "snipid")>
		<cfset URL.method = "getSnippet" />
	<cfelseif NOT structKeyExists(URL, "method")>
		<cfset URL.method = "getSnipEx" />	
	</cfif>
	
	<cfset session.userid = reReplaceNoCase(cgi.PATH_INFO,'/','','all')/>
	
	<cffunction name="getRootID" output="yes" access="remote">
		<cfargument name="userid" default="0">
		<cfset var selectRootID = false />
		<cfquery datasource="#application.dsn#" name="selectRootID">
			SELECT libID FROM lib WHERE parent = 0 AND ownerid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.userID#">
		</cfquery> 
		<cfreturn selectRootID.libID />
	</cffunction>
	
	<cffunction name="getSnipEx" output="yes" access="remote">
		<cfset var _local = structNew() />
		<cfset _local.content = "" />
		<cfset _local.rootID = getRootID(session.userID) />
		<cfset _local.library = queryLibrary( _local.rootID ) />
		<cfset _local.libs = queryLibraries( _local.rootID ) />
		<cfset _local.snips = querySnippets( _local.rootID ) />

		<cfxml variable="_local.content">
			<cfoutput>
			<?xml version="1.0" encoding="utf-8"?>
			<snipex name="#_local.library.name#" id="" createdAt=""  updatedAt="">
				<libraries>
			        <cfloop query="_local.libs">  
						<library id="#_local.libs.libid#" name="#_local.libs.name#" createdAt="#dateFormat(_local.libs.created, "mm-dd-yyyy")#"  updatedAt="#dateFormat(_local.libs.updated, "mm-dd-yyyy")#">
							<![CDATA[#_local.libs.description#]]>
						</library>
					</cfloop>
				</libraries>
				
				<snippets>
					<cfloop query="_local.snips">
				 	<snippet id="#_local.snips.snipid#" template="#_local.snips.template#">
			               <name>#_local.snips.snipname#</name>
			               <help><![CDATA[#_local.snips.help#]]></help>
			               <description><![CDATA[#_local.snips.snipDescription#]]></description>
			               <starttext><![CDATA[#_local.snips.starttext#]]></starttext>
			               <endtext><![CDATA[#_local.snips.endtext#]]></endtext>
			               <author>#_local.snips.author#</author>
			               <platforms>#_local.snips.platforms#</platforms>
			               <created>#dateFormat(_local.snips.libCreateDT, "mm-dd-yyyy")#</created>
			               <lastupdated>#dateFormat(_local.snips.libUpdateDT, "mm-dd-yyyy")#</lastupdated>
			          </snippet>
					</cfloop>
				</snippets>
			</snipex>
			</cfoutput>
		</cfxml>
		
		<cfcontent type="text/xml" reset="true"><cfoutput>#_local.content#</cfoutput>
	</cffunction>
	
	<cffunction name="getLibrary" output="yes" access="remote">
		<cfargument name="libid" />
		
		<cfset var content = "" />
		<cfset var library = queryLibrary( arguments.libid ) />
		<cfset var libs = queryLibraries( arguments.libid ) />
		<cfset var snips = querySnippets( arguments.libid ) />
		
		<cfxml variable="content">
			<cfoutput>
			<?xml version="1.0" encoding="utf-8"?>
			<library name="#library.name#" id="#library.libid#" createdAt="#dateFormat(library.created, "mm-dd-yyyy")#"  updatedAt="#dateFormat(library.updated, "mm-dd-yyyy")#">
				<libraries>
			        <cfloop query="libs">  
						<library id="#libs.libid#" name="#libs.name#" createdAt="#dateFormat(libs.created, "mm-dd-yyyy")#"  updatedAt="#dateFormat(libs.updated, "mm-dd-yyyy")#">
							<![CDATA[#libs.description#]]>
						</library>
					</cfloop>
				</libraries>
				
				<snippets>
					<cfloop query="snips">
				 	<snippet id="#snips.snipid#" template="#snips.template#">
			               <name>#snips.snipname#</name>
			               <help><![CDATA[#snips.help#]]></help>
			               <description><![CDATA[#snips.snipdescription#]]></description>
			               <starttext><![CDATA[#snips.starttext#]]></starttext>
			               <endtext><![CDATA[#snips.endtext#]]></endtext>
			               <author>#snips.author#</author>
			               <platforms>#snips.platforms#</platforms>
			               <created>#dateFormat(snips.snipCreateDT, "mm-dd-yyyy")#</created>
			               <lastupdated>#dateFormat(snips.snipUpdateDT, "mm-dd-yyyy")#</lastupdated>
			          </snippet>
					</cfloop>
				</snippets>
			</library>
			</cfoutput>
		</cfxml>
		
		<cfcontent type="text/xml" reset="true"><cfoutput>#content#</cfoutput>
	</cffunction>
	
	<cffunction name="getSnippet" output="yes" access="remote">
		<cfargument name="snipid" />
		
		<cfset var content = "" />
		<cfset var snippet = querySnippet( arguments.snipid ) />
		
		<cfxml variable="content">
			<cfoutput>
			 	<snippet id="#snippet.snipid#" template="#snippet.template#">
		               <name>#snippet.snipname#</name>
		               <help><![CDATA[#snippet.help#]]></help>
		               <description><![CDATA[#snippet.snipdescription#]]></description>
		               <starttext><![CDATA[#snippet.starttext#]]></starttext>
		               <endtext><![CDATA[#snippet.endtext#]]></endtext>
		               <author>#snippet.author#</author>
		               <platforms>#snippet.platforms#</platforms>
		               <created>#dateFormat(snippet.snipCreateDT, "mm-dd-yyyy")#</created>
		               <lastupdated>#dateFormat(snippet.snipUpdateDT, "mm-dd-yyyy")#</lastupdated>
		          </snippet>
			</cfoutput>
		</cfxml>
		
		<cfcontent type="text/xml" reset="true"><cfoutput>#content#</cfoutput>
	</cffunction>
	
	
	
	<!--- SQL Methods --->
	<cffunction name="queryLibrary" output="yes" access="private">
		<cfargument name="libid" />
		
		<cfset var qResults = "" />

		<cfquery datasource="#application.dsn#" name="qResults">
			SELECT  libid, name, description, parent, created, updated 
			FROM lib 
			WHERE libid = <cfqueryparam value="#arguments.libid#" />
				  AND libid IN (SELECT 	libiD
							  	FROM 	libAccess 
							 	WHERE	userID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.userID#">
								)
		</cfquery>

		<cfreturn qResults />
	</cffunction>

	<cffunction name="querySnippet" output="no" access="private">
		<cfargument name="snipid" />
		
		<cfset var qResults = "" />
		
		<cfquery datasource="#application.dsn#" name="qResults">
			SELECT  t1.snipid, t1.snipname, t1.parent, t1.help, t1.snipDescription, t1.starttext, t1.endtext, t1.author, t1.platforms, t1.template, 
					t1.libCreateDT, t1.libUpdateDT
			FROM snip 
			snip t1 LEFT JOIN
					lib t2 ON t2.libid = t1.parent
					AND t1.snipid = <cfqueryparam value="#arguments.snipid#" />
			WHERE	t2.libid IN   (SELECT 	libiD
							  	FROM 	libAccess 
							 	WHERE	userID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.userID#">
								)
			WHERE 
		</cfquery>
		
		<cfreturn qResults />
	</cffunction>
	
	<cffunction name="queryLibraries" output="no" access="private">
		<cfargument name="libid" />
		
		<cfset var qResults = "" />
		
		<cfquery datasource="#application.dsn#" name="qResults">
			<!--- SELECT libid, name, description, parent, created, updated  
			FROM lib 
			WHERE parent = <cfqueryparam value="#arguments.libid#" /> --->
			SELECT 	 lib.*
			FROM	lib	INNER JOIN 
					libAccess ON lib.libID = libAccess.libID
			WHERE	libAccess.userID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.userid#">
					AND lib.parent = <cfqueryparam value="#arguments.libid#">
		</cfquery>
		
		<cfreturn qResults />
	</cffunction>
	
	<cffunction name="querySnippets" output="no" access="private">
		<cfargument name="libid" />
		
		<cfset var qResults = "" />
		
		<cfquery datasource="#application.dsn#" name="qResults">
			SELECT 	t2.snipid, t2.name as snipName, t2.useremail, t2.help, t2.description as snipDescription, t2.starttext, t2.endtext, 
					t2.author, t2.platforms, t2.template, t2.created as snipCreateDT, t2.updated as snipUpdateDT, 
					t2.published, t2.ownerid as snipOwner, t1.name as libName, t1.description as libDescription, 
					t1.parent as parentLibID, t1.created as libCreateDT, t1.updated as libUpdateDT, t1.isPublic, 
					t1.ownerID as libOwnerID, t1.libID
			FROM	snip t2 LEFT JOIN
					lib t1 ON t1.libid = t2.parent
					<cfif arguments.libid gt 0>
					AND t1.libID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.libID#">
					</cfif>
			WHERE	libid IN   (SELECT 	libiD
							  	FROM 	libAccess 
							 	WHERE	userID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.userID#">
								)
			ORDER BY t1.libID, t2.snipID
			<!--- SELECT snipid, name, parent, help, description, starttext, endtext, author, platforms, template, created, updated
			FROM snip 
			WHERE parent = <cfqueryparam value="#arguments.libid#" />
			AND published = 1 --->
		</cfquery>
		
		<cfreturn qResults />
	</cffunction>
	
	
	<cffunction name="submitSnippet" output="true" access="remote">
		
		<cftry>
		
			<cfquery datasource="#application.dsn#" name="qInsert">
				INSERT INTO snip(name, description, starttext, endtext, parent,author, useremail, help, published, created, updated)
				VALUES(
					<cfqueryparam value = "#arguments.snippetname#" CFSQLType = "cf_sql_varchar">,
					<cfqueryparam value = "#arguments.snippetdescription#" CFSQLType = "cf_sql_varchar">,
					<cfqueryparam value = "#arguments.snippetstartblock#" CFSQLType = "cf_sql_varchar">,
					<cfqueryparam value = "#arguments.snippetendblock#" CFSQLType = "cf_sql_varchar">,
					<cfqueryparam value = "#arguments.libraryid#" CFSQLType = "cf_sql_integer">,
					<cfqueryparam value = "#arguments.username#" CFSQLType = "cf_sql_varchar">,
					<cfqueryparam value = "#arguments.useremail#" CFSQLType = "cf_sql_varchar">,
					'',
					
					<cfif application.moderate>
						<cfqueryparam value = "0" CFSQLType = "cf_sql_integer">,
					<cfelse>
						<cfqueryparam value = "1" CFSQLType = "cf_sql_integer">	,
					</cfif>
					
					<cfqueryparam value="#createodbcdatetime(Now())#" cfsqltype="cf_sql_timestamp">,
					<cfqueryparam value="#createodbcdatetime(Now())#" cfsqltype="cf_sql_timestamp">
				)
				
			</cfquery>
			<cfoutput>Snippet has been submitted</cfoutput>
		<cfcatch>
					<cfoutput>#CFCATCH.Message#</cfoutput>
					<cfdump var="#CFCATCH#">		
		</cfcatch>
		</cftry>
		

	</cffunction>
	
	<cffunction name="canSubmit" access="remote" output="true">
		<cfparam name="application.canSubmit" default="true">
		
		<cfoutput>#application.canSubmit#</cfoutput>
	</cffunction>
	
	
	
</cfcomponent>
